package com.shujia.LoginDemo;
import com.shujia.utils.MySQLTool;
import com.shujia.utils.SendYzm;
import redis.clients.jedis.Jedis;

import java.sql.*;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        init();
//        login();
//        register();
        forget();

    }

    public static void init() {
        System.out.println("===========欢迎来到尚平婚恋介绍所==========");
        System.out.println("请选择您要做的操作：(1.登录  2.注册  3.忘记密码)");
        Scanner sc = new Scanner(System.in);
        String choice = sc.nextLine();
        switch (choice) {
            case "1":
                //TODO:登录

                break;
            case "2":
                //TODO:注册
                break;
            case "3":
                //TODO:忘记密码
                break;
            default:
                System.out.println("没有该选项，已退出....");
        }
    }


    public static void register(){
        Connection conn = null;
        Statement statement = null;
//        ResultSet resultSet = null;
        while (true){
            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("*********注册页面*******");
                System.out.println("请输入您的注册用户名:");
                String username = sc.next();
                //先查询数据库，判断该用户是否存在
                //使用工具类获取连接对象
                conn = MySQLTool.getMysqlConnection();
//            System.out.println(conn);
                statement = conn.createStatement();
                String sql = "select name,password from users where name='\" + username + \"'";
                ResultSet resultSet = statement.executeQuery(sql);
//                String pad = resultSet.getString("password");
//            System.out.println(resultSet);


                if (resultSet.next()) {
                    System.out.println("用户名已经被占用，请重新输入！！");
                }else {
                    System.out.println("请输入您的注册密码：");
                    String password = sc.next();
                    System.out.println("请输入您的注册号码：");
                    String phoneNumber = sc.next();
                    int i = statement.executeUpdate("insert into users(name,password,phoneNumber) values('" + username + "','" + password + "','" + phoneNumber + "')");
                    if (i == 1) {
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }
                    break;

                }
//                if (resultSet.next()) {
//                    System.out.print("请输入您的注册密码:");
//                    String password = sc.next();
//                    if (pad.equals(password)) {
//                        System.out.println("注册成功！！爽去吧，老东西！！");
//                    } else {
//                        System.out.println("该用户还未注册！！请先注册！！");
//                    }
//                }else {
//                    System.out.println("排错");
//                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }


    public static void login() {
        Connection conn = null;
        Statement statement = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("*********登录页面*******");
            System.out.println("请输入您的用户名:");
            String username = sc.next();
            System.out.print("请输入您的密码:");
            String password = sc.next();
            //先查询数据库，判断该用户是否存在
            //使用工具类获取连接对象
            conn = MySQLTool.getMysqlConnection();
            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("select name,password from users where name='" + username + "'");
            if (resultSet.next()) {
                String pad = resultSet.getString("password");
                if (password.equals(pad)) {
                    System.out.println("登录成功！！找尚平结婚去吧！！");
                } else {
                    System.out.println("该用户还未注册！！请先注册！！");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    public static void forget(){//实现忘记密码功能
        Statement statement = null;
        Connection conn = null;
        Jedis jedis = new Jedis("192.168.11.100", 8000);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String username = sc.next();
        System.out.println("请输入您的手机号码：");
        String phoneNumber = sc.next();
        String num =SendYzm.Send();
        jedis.set(num,phoneNumber);
        jedis.expire(num,300);
        System.out.println("【尚平婚恋所】您的验证码为："+num+"有效期为5分钟");
        while (true){
            try {
                
                conn = MySQLTool.getMysqlConnection();
                statement = conn.createStatement();
                ResultSet resultSet = statement.executeQuery("select password from users where name='" + username + "'");
                System.out.println("请输入您收到的验证码：");
                String yzm = sc.next();
                if (yzm.equals(jedis.keys(num))) {
                    String pad = resultSet.getString("password");
                    System.out.println(pad);
                }else {
                    System.out.println("验证码错误，请重新输入！！");
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            break;
        }


    }


    public static void lianjie(){//连接redis接口
        Jedis jedis = new Jedis("192.168.11.100", 8000);

    }

}
